<template>
	<view class="pages">
		<!-- 顶部空余 -->
		<view style="width: 100%;height: var(--status-bar-height)"></view>
		<view class="top_fixed">
			<view class="search_nav">
				<view class="search_content">
					<view class="left">
						<uni-icons type="back" @click="back" size="22"></uni-icons>
					</view>
					<view class="center">
						<uni-icons type="search" color="#646464" style='margin-left: 20rpx;margin-right: 8rpx;' size="20"></uni-icons>
						<input type="text" v-model="keywords" style="width: 80%;">
					</view>
					<view class="right" @click="searchGoods">{{$t('index.search.Search')}}</view>
				</view>
			</view>
			<view class="tab_swiper">
				<view @click="changeTab(item)" :class="activeType==item.id?'active_tab_item tab_item':'tab_item'"
					v-for="item in tabList">
					<view class="text">{{item.name}}</view>
					<view class="tab_price_img" v-show="item.switch" @click="changeAdd">
						<uni-icons v-show="add" type="top" size="15" :color="activeType==2?'':'#a2a2a2'"></uni-icons>
						<uni-icons v-show="!add" type="bottom" size="15" :color="activeType==2?'':'#a2a2a2'"></uni-icons>
					</view>
				</view>
			</view>
		</view>
		<view class="search_goods">
			<goods :goodList='goodList'></goods>
		</view>
	</view>
</template>

<script>
	import { goodList } from '@/api/api.js'
	import goods from '@/components/shop/goods/goods.vue'
	export default {
		components: {
			goods
		},
		data() {
			return {
				keywords: '',
				// 价格增序
				add: true,
				// 传来的搜索条件
				dataCondition: {},
				page: 1,
				listorder: 'default',
				goodList: [],
				isPriceAdd: true,
				activeType: 0,
				tabList: [{
						id: 0,
						name: this.$t('index.search.Synthesis'),
						listorder: 'default'
					},
					{
						id: 1,
						name: this.$t('index.search.Sales'),
						listorder: 'sales'
					},
					{
						id: 2,
						name: this.$t('index.search.Price'),
						switch: true,
						listorder: 'pricedesc'
					},
					{
						id: 3,
						name: this.$t('index.search.New'),
						listorder: 'new'
					}
				],
        receiveId:'' //从分类页跳转的得到的id
			}
		},
		onLoad(item) {
      if(item.source){
        this.receiveId = item.id
        this.dataCondition.second_id =  this.receiveId
        this.dataCondition.id = null
        this.getGoodList()
        return
      }
			if(Object.keys(item).length!=0){
				this.dataCondition = JSON.parse(item.idArr)
				if(this.dataCondition.first_id == 0){
					this.dataCondition.first_id = this.dataCondition.id
					this.dataCondition.second_id =  0
					this.dataCondition.id = null
				}
				if(this.dataCondition.second_id == 0){
					this.dataCondition.second_id = this.dataCondition.id
					this.dataCondition.id = null
				}
			}
			this.getGoodList()
		},
		//触底加载更多
		onReachBottom() {
			this.page++;
			this.getGoodList()
		},
		methods: {
			searchGoods() {
				// 先清空商品列表防止叠加
				this.goodList = []
				this.getGoodList()
			},
			getGoodList() {
				goodList({
					first_id: this.dataCondition.first_id,
					second_id: this.dataCondition.second_id,
					cate_id: this.dataCondition.id,
					page: this.page,
					keywords: this.keywords,
					listorder: this.listorder
				}).then(res => {
					if (res.data.data.data.length > 0) {
						this.goodList = this.goodList.concat(res.data.data.data);
					} else {
						this.page--;
						uni.showToast({
							title: this.$t('general.There'),
							icon: "none"
						})
					}
				})
			},
			back() {
				uni.navigateBack()
			},
			changeTab(item) {
				// 先清空商品列表防止叠加
				this.goodList = []
				this.listorder = item.listorder
				this.getGoodList()
				this.activeType = item.id
			},
			changeAdd() {
				if (this.add) {
					this.add = false
					this.tabList[2].listorder = 'pricedesc'
				} else {
					this.add = true
					this.tabList[2].listorder = 'priceasc'
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.top_fixed {
		position: fixed;
		z-index: 99;
		top: 0;
		left: 0;
		right: 0;
	}

	.search_nav {
		width: 100%;
		height: 108rpx;
		display: flex;
		align-items: center;
		background: linear-gradient(#edf5f8, #f4f8fb);

		.search_content {
			width: 95%;
			height: 68rpx;
			margin: 20rpx auto;
			display: flex;
			align-items: center;
			justify-content: space-between;
			font-size: 28rpx;

			.left {
				width: 5%;
			}

			.center {
				width: 80%;
				height: 68rpx;
				background-color: #fff;
				border-radius: 8rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;

				input {
					color: #919191;
					font-size: 28rpx;
				}
			}

			.right {
				width: 10%;
			}
		}
	}

	.tab_swiper {
		width: 100%;
		height: 90rpx;
		display: flex;
		background-color: #fff;
		align-items: center;
		justify-content: space-around;

		.tab_item {
			display: flex;
			align-items: center;
			color: #a2a2a2;
			font-weight: bold;
			font-size: 30rpx;

			.tab_price_img {
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				margin-left: 5rpx;
			}
		}

		.active_tab_item {
			color: #000000;
		}
	}

	.search_goods {
		padding-top: 220rpx;
		padding-bottom: 60rpx;
	}
</style>